package small.common.executeEngine.engine;

import small.common.executeEngine.processor.TaskProcessor;
import small.common.executeEngine.task.ExecuteTask;
import small.common.log.LogProvider;
import org.slf4j.Logger;

import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractTaskExecuteEngine<T extends ExecuteTask> implements TaskExecuteEngine<T> {

    private final ConcurrentHashMap<Object, TaskProcessor> taskProcessors = new ConcurrentHashMap();

    private TaskProcessor defaultTaskProcessor;

    protected final Logger log;

    public AbstractTaskExecuteEngine(Logger logger) {
        this.log = null != logger ? logger : LogProvider.getLogger(AbstractTaskExecuteEngine.class);
    }

    @Override
    public TaskProcessor getProcessor(Object key) {
        return taskProcessors.containsKey(key) ? taskProcessors.get(key) : defaultTaskProcessor;
    }

    @Override
    public void addProcessor(Object key, TaskProcessor taskProcessor) {
        //如果不存在则放入执行器
        taskProcessors.putIfAbsent(key, taskProcessor);
    }

    @Override
    public void removeProcessor(Object key) {
        taskProcessors.remove(key);
    }

    @Override
    public void setDefaultTaskProcessor(TaskProcessor defaultTaskProcessor) {
        this.defaultTaskProcessor = defaultTaskProcessor;
    }


}
